package com.tt.fashionWebSite.dao.jpa;

import com.tt.fashionWebSite.dao.ChiTietSanPhamDao;
import com.tt.fashionWebSite.dao.SanPhamFullDao;
import com.tt.fashionWebSite.domain.SanPhamFull;
import com.tt.fashionWebSite.dto.ChiTietSanPhamFullDto;
import com.tt.fashionWebSite.dto.ChiTietSanPhamRuleDto;
import com.tt.fashionWebSite.dto.SanPhamFullDto;
import com.tt.fashionWebSite.dto.SanPhamRuleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by PC on 11/28/14.
 */
@Repository("sanPhamFull")
public class SanPhamFullJpaDao extends GenericJpaDao<SanPhamFull,String> implements SanPhamFullDao {
   @Autowired
   @Qualifier("chiTietSanPham")
   private ChiTietSanPhamDao chiTietSanPhamDao;

   public SanPhamFullJpaDao(){
      super(SanPhamFull.class);
   }

//   @Override
//   public List<SanPhamFull> getProductByIdDMSP(String gender, String idDMSP) {
//      return getEntityManager().createNamedQuery(SanPhamFull.GET_PRODUCT_BY_DMSP, SanPhamFull.class)
//            .setParameter("gender", gender)
//            .setParameter("idDMSP", idDMSP)
//            .getResultList();
//   }

   @Override
   public List<SanPhamFullDto> getProductByIdDMSP(String gender, String idDMSP) {
      String strQuery = "SELECT "
            + "s.MA_SP, "
            + "s.TEN_SP, "
            + "s.THONG_TIN, "
            + "s.DOI_TUONG "
            + "FROM san_pham s "
            + "WHERE s.DOI_TUONG = :gender AND s.MA_LOAI_DM_SP =:idDMSP";

      Query query = getEntityManager().createNativeQuery(strQuery);
      query.setParameter("gender", gender);
      query.setParameter("idDMSP", idDMSP);

//      query.setMaxResults(20);
//      query.setFirstResult(60);

      List<Object> results = query.getResultList();
      List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
      SanPhamFullDto sanPhamFullDto = null;
      for (int i = 0; i < results.size(); i++) {
         Object[] userDB = (Object[]) results.get(i);
         sanPhamFullDto = new SanPhamFullDto(userDB);

         //      get product detail
         List<ChiTietSanPhamFullDto> chiTietSanPhamFullDtos = chiTietSanPhamDao.getAllProductDetailByMaSP(userDB[0].toString());

         sanPhamFullDto.setChiTietSanPhams(chiTietSanPhamFullDtos);
         sanPhamFullDtos.add(sanPhamFullDto);

      }

      return sanPhamFullDtos;
   }

   @Override
   public List<SanPhamFull> getProductAllByGender(String gender) {
      return getEntityManager().createNamedQuery(SanPhamFull.GET_ALL_BY_GENDER, SanPhamFull.class)
            .setParameter("gender", gender)
            .getResultList();
   }

   @Override
   public List<SanPhamFull> getProductSearch(String name) {
      return getEntityManager().createNamedQuery(SanPhamFull.GET_SEARCH, SanPhamFull.class)
            .setParameter("name", name + LIKE_CHARACTER)
            .getResultList();
   }

   @Override
   public List<SanPhamRuleDto> getProductAll() {
      String strQuery = "SELECT "
            + "s.MA_SP, "
            + "s.TEN_SP "
            + "FROM san_pham s ";

      Query query = getEntityManager().createNativeQuery(strQuery);
      List<Object> results = query.getResultList();
      List<SanPhamRuleDto> hoaDonFullDtos = new ArrayList<SanPhamRuleDto>();
      SanPhamRuleDto hoaDonFullDto = null;
      for (int i = 0; i < results.size(); i++) {
         Object[] userDB = (Object[]) results.get(i);
         hoaDonFullDto = new SanPhamRuleDto(userDB);

         List<ChiTietSanPhamRuleDto> chiTietSanPhamRuleDtos = chiTietSanPhamDao.getDetailProductForRule(userDB[0].toString());

         hoaDonFullDto.setChiTietSanPhams(chiTietSanPhamRuleDtos);
         hoaDonFullDtos.add(hoaDonFullDto);
      }
      return hoaDonFullDtos;
   }

    @Override
    public List<SanPhamFull> getDataDetailForDeal(String color, String motif, String size) {
        return getEntityManager().createNamedQuery(SanPhamFull.GET_PRODUCT_FOR_DEAL, SanPhamFull.class)
                .setParameter("color", color)
                .setParameter("motif", motif)
                .setParameter("size", size)
                .getResultList();
    }
}
